package com.mrdeer.web.service.sys.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mrdeer.common.entity.beans.PageEntity;
import com.mrdeer.common.entity.dto.PermissionDTO;
import com.mrdeer.common.entity.sys.Permission;
import com.mrdeer.common.exception.BusinessException;
import com.mrdeer.web.mapper.PermissionMapper;
import com.mrdeer.web.service.sys.SysPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
@Transactional
public class SysPermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements SysPermissionService {

    @Resource
    private PermissionMapper permissionMapper;

    @Override
    public Permission add(Permission permission) {
        Permission p = getById(permission.getId());
        if (null != p) {
            throw new BusinessException("The permission exists in the database");
        }
        if (permissionMapper.insert(permission) > 0) {
            return permissionMapper.selectById(permission.getId());
        }
        throw new BusinessException("add permission failed");
    }

    @Override
    public boolean delete(String id) {
        Permission p = getById(id);
        if (null == p) {
            throw new BusinessException("the permission does not exist");
        }
        return permissionMapper.deleteById(id) > 0;
    }

    @Override
    public Permission update(Permission permission) {
        if (permissionMapper.updateById(permission) > 0) {
            Permission select = permissionMapper.selectById(permission.getId());
            return select;
        }
        throw new BusinessException("The permission failed in the database");
    }

    @Override
    public Permission getById(String id) {
        return permissionMapper.selectById(id);
    }

    @Override
    public Page<Permission> list(PermissionDTO dto) {
        if (null == dto.getPage()) {
            dto.setPage(new PageEntity());
        }
        return permissionMapper.selectPage(dto.getPage().convert(), fuzzyQuery(dto));
    }

    @Override
    public LambdaQueryWrapper<Permission> fuzzyQuery(PermissionDTO dto) {
        LambdaQueryWrapper<Permission> queryWrapper = Wrappers.<Permission>query().lambda();
        if (null != dto) {
            if (StrUtil.isNotBlank(dto.getTitle())) {
                queryWrapper.like(Permission::getTitle, dto.getTitle());
            }
            if (StrUtil.isNotBlank(dto.getParentId())) {
                queryWrapper.eq(Permission::getParentId, dto.getParentId());
            }
            if (dto.getType() != null) {
                queryWrapper.eq(Permission::getType, dto.getType());
            }
            if (dto.getStatus() != null) {
                queryWrapper.eq(Permission::getStatus, dto.getStatus());
            }
            if (dto.getTitle() != null) {
                queryWrapper.like(Permission::getTitle, dto.getTitle());
            }
        }
        return queryWrapper;
    }

    @Override
    public List<Permission> findByUserId(String userId) {
        return permissionMapper.findByUserId(userId);
    }

    @Override
    public List<Permission> findByParentIdOrderBySortOrder(String parentId) {
        PermissionDTO dto = new PermissionDTO();
        dto.setParentId(parentId);
        return permissionMapper.selectList(fuzzyQuery(dto));
    }

    @Override
    public List<Permission> findByTypeAndStatusOrderBySortOrder(Integer type, Integer status) {
        PermissionDTO dto = new PermissionDTO();
        dto.setType(type);
        dto.setStatus(status);
        return permissionMapper.selectList(fuzzyQuery(dto));
    }

    @Override
    public List<Permission> findByTitle(String title) {
        PermissionDTO dto = new PermissionDTO();
        dto.setTitle(title);
        return permissionMapper.selectList(fuzzyQuery(dto));
    }

    @Override
    public List<Permission> findByTitleLikeOrderBySortOrder(String title) {
        PermissionDTO dto = new PermissionDTO();
        dto.setTitle(title);
        return permissionMapper.selectList(fuzzyQuery(dto));
    }
}
